package fi.jamk.ourcookingapp.activity;

import java.util.concurrent.ExecutionException;

import org.json.JSONException;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import fi.jamk.ourcookingapp.R;
import fi.jamk.ourcookingapp.backend.MyTools;
import fi.jamk.ourcookingapp.backend.exceptions.EmptyInputException;
import fi.jamk.ourcookingapp.backend.exceptions.NoUserException;
import fi.jamk.ourcookingapp.backend.exceptions.WebServiceException;
import fi.jamk.ourcookingapp.backend.model.User;

/**
 * Activity for handling the user login, register
 * @author Marius
 */
public class MainActivity extends Activity {

	private boolean register = false;
	
	private TextView txtEmail;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		AppActivity.facade.setResources(getResources());
		try {
			// try to login with the stored user
			login();
		} catch (NoUserException | EmptyInputException | InterruptedException | ExecutionException | JSONException | WebServiceException e) {
			// if login is failed show login formular
			initLogin();
		}
	}
	
	/**
	 * Login user with stored data
	 * @throws NoUserException
	 * @throws EmptyInputException
	 * @throws InterruptedException
	 * @throws ExecutionException
	 * @throws JSONException
	 * @throws WebServiceException
	 */
	private void login() throws NoUserException, EmptyInputException, InterruptedException, ExecutionException, JSONException, WebServiceException {
		SharedPreferences user = getSharedPreferences(AppActivity.SAVED_USER, 0);
		String loginName = user.getString(AppActivity.SAVED_USER_NAME, "");
		String password = user.getString(AppActivity.SAVED_USER_PASSWORD, "");
		int id = user.getInt(AppActivity.SAVED_USER_ID, 0);
		loginUser(loginName, password, id);
	}

	private void loginUser(String userName, String password, int id) throws NoUserException, EmptyInputException, InterruptedException, ExecutionException, JSONException, WebServiceException {
		User user = new User(userName);
		user.setPassword(password);
		user.setID(id);
		AppActivity.facade.login(user);
		Intent intent = new Intent(MainActivity.this, AppActivity.class);
		startActivity(intent);
		finish();
	}
	
	/**
	 * Initialize functionality for login/register
	 */
	private void initLogin() {
		final CheckBox cbRegister = (CheckBox) findViewById(R.id.cbRegister);
		final EditText txtUsername = (EditText) findViewById(R.id.userName);
		final EditText txtPassword = (EditText) findViewById(R.id.password);
		final Button confirm = (Button) findViewById(R.id.btnLogin);
		
		cbRegister.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(cbRegister.isChecked()){
					confirm.setText(getResources().getString(R.string.btnRegister));
					initRegister(true);
				} else {
					confirm.setText(getResources().getString(R.string.btnLogin));
					initRegister(false);
				}
			}
		});
		
		confirm.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String userName = txtUsername.getText().toString();
				String password = txtPassword.getText().toString();
				
				if(MyTools.isEmpty(userName) || MyTools.isEmpty(password)){
					Toast.makeText(MainActivity.this, getResources().getString(R.string.emptyUserPW), Toast.LENGTH_SHORT).show();
				} else {
					try {
						if(register){
							createUser(userName, password);
						} else {
							loginUser(userName, password, 0);
						}
					} catch (NoUserException | EmptyInputException | InterruptedException | ExecutionException | JSONException | WebServiceException e) {
						Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
					}
				}
			}

		});
	}

	protected void createUser(String userName, String password) throws NoUserException, EmptyInputException, InterruptedException, ExecutionException, JSONException, WebServiceException {
		User user = new User(userName);
		user.setPassword(password);
		user.setEMail(txtEmail.getText().toString());
		AppActivity.facade.createUser(user);
		Intent intent = new Intent(MainActivity.this, AppActivity.class);
		startActivity(intent);
		finish();
	}

	protected void initRegister(boolean b) {
		register = b;
		LinearLayout ll = (LinearLayout) findViewById(R.id.llEmail);
		if(b){
			TextView lblEmail = new TextView(this);
			lblEmail.setText("Email");
			ll.addView(lblEmail);
			txtEmail = new EditText(this);
			ll.addView(txtEmail);
		} else {
			ll.removeAllViews();
		}
	}
	
}
